package com.caifu.timer;

import com.caifu.mapper.UserInfoMapper;
import com.caifu.mapper.YlElderlyInfoMapper;
import com.caifu.pojo.UserInfo;
import com.caifu.pojo.YlElderlyInfo;
import com.caifu.service.ElderlyInfoService;
import com.caifu.util.AesMyUtil;
import com.caifu.util.CheckIdCardUtil;
import com.caifu.util.Util;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * ClassName : UserInfoTimer
 *
 * @author :  yb
 * @description ：
 * @date : 2020-11-19 15:48
 */

@Configuration
@EnableScheduling
public class Timer {


    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private YlElderlyInfoMapper ylElderlyInfoMapper;
    @Resource
    private ElderlyInfoService elderlyInfoService;


    /**
     * 老人档案和老人信息 计算年龄
     * (fixedRate = 2000)
     */
    @Scheduled(cron = "5 0 0 * * ?")
    private void configureTasks() {
        try {
            String today = LocalDate.now().format(DateTimeFormatter.ofPattern("MMdd"));

            //老人档案 计算年龄
            List<Map<String, Object>> userInfoList = userInfoMapper.queryUserInfoByBirth(today);
            userInfoList.forEach(map -> {
                String userNo = map.get("userNo").toString();
                UserInfo userInfo = userInfoMapper.selectById(userNo);
                if (Util.isNotNull(userInfo.getBirthDate())) {
                    userInfo.setAge(CheckIdCardUtil.getAgeByBirthDate(userInfo.getBirthDate()));
                } else {
                    userInfo.setAge(userInfo.getAge() + 1);
                }
                userInfoMapper.updateById(userInfo);
            });

            //老人信息处理
            List<Map<String, Object>> elderlyInfoList = ylElderlyInfoMapper.queryElderlyInfoByBirth(today);
            elderlyInfoList.forEach(map -> {
                String userNo = map.get("userNo").toString();
                YlElderlyInfo elderlyInfo = ylElderlyInfoMapper.selectById(userNo);
                try {
                    elderlyInfo.setAge(CheckIdCardUtil.getAgeByCertId(Objects.requireNonNull(AesMyUtil.Decrypt(elderlyInfo.getUserPid()))));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ylElderlyInfoMapper.updateById(elderlyInfo);
            });

            System.out.println("执行静态定时任务时间: " + LocalDateTime.now() + "---老人档案和养老院老人信息-计算年龄");
        } catch (Exception e) {
            System.out.println("定时器计算年龄 错误：" + e.getMessage());
            e.printStackTrace();
        }
    }


    /**
     * 计算各种合同状态
     * (fixedRate = 2000)
     */
    @Scheduled(cron = "10 15 0 * * ?")
    private void anyRecordTasks() {
        try {
            //床位记录
            List<String> bedUserNoList = ylElderlyInfoMapper.getUserNoForTimerWithBedRecord(null);
            bedUserNoList.forEach(userNo -> elderlyInfoService.updateBedRecordByEffectDate(userNo));

            //护理记录
            List<String> nursingUserNoList = ylElderlyInfoMapper.getUserNoForTimerWithNursingRecord(null);
            nursingUserNoList.forEach(userNo -> elderlyInfoService.updateNursingRecordByEffectDate(userNo));

            //评估记录
            List<String> assessmentUserNoList = ylElderlyInfoMapper.getUserNoForTimerWithAssessmentRecord(null);
            assessmentUserNoList.forEach(userNo -> elderlyInfoService.updateAssessmentRecordByEffectDate(userNo));


            System.out.println("执行静态定时任务时间: " + LocalDateTime.now() + "---定时器计算各种合同状态");
        } catch (Exception e) {
            System.out.println("定时器计算各种合同状态 错误：" + e.getMessage());
            e.printStackTrace();
        }
    }


}
